<script setup lang="ts">
import { useLoadMore } from 'vue-request'
import { fetchPointsRecordAPI } from '@/apis/fetchPointsRecord'
import { useUserInfoStore } from '@/store/userInfo'

const userInfoStore = useUserInfoStore()

// 分页列表数据
const { dataList, loading, loadingMore, noMore, loadMore } = useLoadMore<LoadMoreData<PointsRecord>>(
  fetchData,
  { isNoMore: d => d?.list.length === d?.total },
)

// “加载更多”的状态
const loadMoreStatus = computed(() => {
  if (loadingMore.value)
    return 'loading'
  else if (noMore.value)
    return 'nomore'
  else
    return 'loadmore'
})

onReachBottom(() => loadMore())

/**
 * @description 分页服务函数
 */
async function fetchData(d?: LoadMoreData<PointsRecord>): Promise<LoadMoreData<PointsRecord>> {
  // 当前页数
  const _page = d?.page ? d.page + 1 : 1

  const res = await fetchPointsRecordAPI(
    { page: _page.toString(), limit: '10' },
    userInfoStore.userInfo!.token!,
  )

  return {
    list: res.data?.data.data || [],
    page: _page,
    total: res.data?.data.total || 9999,
  }
}
</script>

<template>
  <Spacer height="40" />
  <view v-if="dataList.length > 0">
    <view v-for="item in dataList" :key="item.id" class="mb-24rpx flex items-center justify-between rounded-20rpx bg-white p-24rpx">
      <view>
        <view class="text-32rpx text-#333333 font-semibold leading-45rpx">
          {{ item.title }}
        </view>
        <Spacer height="20" />
        <view class="text-24rpx text-#333333 leading-32rpx">
          {{ item.create_time }}
        </view>
      </view>
      <view
        class="flex-shrink-0 text-36rpx font-medium leading-50rpx"
        :style="{ color: item.type.toString() === '1' ? '#1B85FF' : '#FF7419' }"
      >
        {{ `${item.type.toString() === '1' ? '+' : '-'}${parseInt(item.score)}积分` }}
      </view>
    </view>
    <!-- 加载更多 -->
    <uv-load-more v-if="dataList.length > 3" :status="loadMoreStatus" @loadmore="loadMore" />
    <Spacer height="240" />
  </view>
  <PageDefault v-else>
    暂无积分明细
  </PageDefault>
  <uv-loading-page :loading="loading && !loadingMore" loading-text="加载中..." font-size="28rpx" loading-mode="spinner" />
</template>

<route lang="yaml">
name: 'pointsStatement'
style:
  navigationBarTitleText: '积分明细'
</route>
